package com.cksys.achievement.config;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName: AjaxAuthSuccessHandler
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2020/4/4 19:26
 */
@Component
public class AjaxAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String json = "{\"payload\":\"登录成功！\",\"success\":true,\"msg\":\"登录成功！\",\"code\":200}";
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();

        System.out.println(SecurityContextHolder.getContext().getAuthentication());

        out.write(json);
        out.flush();
        out.close();
    }

}
